package com.sysu.homework.DAO.Impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.sysu.homework.DAO.GroupDAO;
import com.sysu.homework.model.Group;
import com.sysu.homework.model.Student;

@Component("groupDAO")
public class GroupDAOImpl implements GroupDAO {
	private HibernateTemplate hibernateTemplate;

	@Override
	public Group getGroupById(int id) {
		return (Group)hibernateTemplate.get(Group.class, id);
	}

	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	@Override
	public int removeGroup(int groupId) {
		Group group = getGroupById(groupId);
		hibernateTemplate.delete(group);
		hibernateTemplate.flush();
		return 1;
	}

	@Override
	public int setLockToGroup(Group group) {
		hibernateTemplate.saveOrUpdate(group);
		hibernateTemplate.flush();
		return 1;
	}

	@Override
	public List<Student> getGroupStudentsById(int groupid) {
		List<Student> students;
		students = hibernateTemplate.find("from Student student where student.group.id='"+groupid+"'");
		return students;
	}

	@Override
	public List<Group> getAllGroups() {
		List<Group> groups;
		groups = hibernateTemplate.find("from Group g order by g.id");
		return groups;
	}

}
